package org.liang.factory.code;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.liang.factory.bean.BeanAttrsInfo;
import org.liang.factory.bean.BeanInfo;

public class CreateExampleJavaCode {

	public static String path="D:\\workspace\\jixiang2\\jx-air\\jx-air-module-jp\\src\\main\\java";
	public static String pg="com.juneyaoair.jp.biz.dto.flightdynamic";
	
	public static void main(String[] args) {
		String p = path+"\\"+pg.replace(".", "\\");
		File f = new File(p);
		File[] fs = f.listFiles(new FilenameFilter() {
			
			public boolean accept(File dir, String name) {
				return name.endsWith(".java");
			}
		});
		for (int i = 0; i < fs.length; i++) {
			File file = fs[i];
			createOne(file);
		}
		
	}
	
	public static List<BeanInfo> createList() {
		List<BeanInfo> list = new ArrayList<BeanInfo>();
		String p = path+"\\"+pg.replace(".", "\\");
		File f = new File(p);
		File[] fs = f.listFiles(new FilenameFilter() {
			
			public boolean accept(File dir, String name) {
				return name.endsWith(".java");
			}
		});
		for (int i = 0; i < fs.length; i++) {
			File file = fs[i];
			list.add(createOne(file));
		}
		
		return list;
	}
	
	public static BeanInfo createOne(File f) {
		BeanInfo beanInfo = new BeanInfo();
		try {
			beanInfo.setClassPackage(pg);
			beanInfo.setClassPath(path);
			beanInfo.cover=true;
			List<BeanAttrsInfo> attrList = new ArrayList<BeanAttrsInfo>();
			
			Scanner sc = new Scanner(f);
			String line=null;
			while(sc.hasNextLine()) {
				line =sc.nextLine();
				if(line.matches(".*class.*")) {
					String[] t=line.split("\\s+");
					beanInfo.setClassName(t[2]);
				}else if(line.matches(".*private.*")) {
					String[] t1=line.split("//");
					String comment = t1.length > 1?t1[1]:"";
					
					String[] t2=t1[0].split("\\s+");
					String type=t2[2];
					String attr=t2[3].replace(";", "");
					
//					System.out.println(attr+"-->" + comment);
					attrList.add(new BeanAttrsInfo(attr,type,comment));
				}
				
			}
			
			beanInfo.setAttrList(attrList);
			sc.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return beanInfo;
	}
	
}
